<?php


namespace app\app\controller;


use think\Db;

class Shop extends WxUser
{
    protected $shopInfo;
    protected $shopId;

    protected function initialize()
    {
        parent::initialize();
        $shop = Db::name('shop')
            ->where(['member_id' => $this->memberId, 'is_del' => 0])
            ->find();
        if (!$shop) {
            responseJson(-300, '商家信息不存在');
        }
        if ($shop['status'] != 1) {
            responseJson(-400, '商家状态异常，请联系管理员');
        }
        $this->shopInfo = $shop;
        $this->shopId = $shop['id'];
    }

    //获取商户信息
    public function getShopInfo()
    {
        msg(0, '查询完成', $this->shopInfo);
    }

    //修改商户信息
    public function editShopInfo()
    {
        $data = $this->request->param();
        $update_data = array(
            'id' => $this->shopId,
            'address' => $data['address'],
            'people' => $data['people'],
            'phone' => $data['phone'],
            'latitude'=>$data['latitude'],
            'longitude'=>$data['longitude'],
        );
        $data_list = getAddressByLocation2($data['latitude'], $data['longitude']);
        $data_list = json_decode($data_list, true);
        if ($data_list['code'] == 0) {
            $address_info=$data_list['data']['address_component'];
            $update_data['province']=$address_info['province'];
            $update_data['city']=$address_info['city'];
            $update_data['area']=$address_info['district'];
        }
        Db::startTrans();
        try {
            $business2_id = Db::name('shop')->update($update_data);
            Db::commit();
            msg(0, '查询成功', $this->shopInfo);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //核销订单列表
    public function getOrderList()
    {

        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $time = $data['time'];
        $where[] = ['o.shop_id', '=', $this->shopId];
        if (isset($time)&&$time){
            $order_time=getStrtoTime($time);
            $where[] = ['o.pay_time', '<=', $order_time[1]];
        }

        //1待支付 2待发货 3待收货 4已完成 5已评价 6售后退货
        //7已取消 8待取货 9拼团待完成10砍价发起
        if ($data['type'] == 0) {
            $where[] = ['o.status', 'in', [4, 5, 8]];
        }
        if ($data['type'] == 1) {
            $where[] = ['o.status', 'in', [8]];
        }
        if ($data['type'] == 2) {
            $where[] = ['o.status', 'in', [4, 5]];
        }
        $order_list = Db::name('order')->alias('o')
            ->join('order_goods og', 'og.order_id=o.id')
            ->field('o.*,og.suk,og.goods_id,og.num,og.price,og.goods_name,og.goods_img,og.sum_money')
            ->where($where)
            ->order('create_time desc')
            ->paginate($length, false, ['page' => $page])
            ->each(function ($item) {
//                if (!$item['shop_get_money'] == 0) {
//                    $goods_info=Db::name('goods')->find($item['goods_id']);
//                    $item['shop_get_money']=$item['sum_money']*($goods_info['shop_return']/100);
//                }
                $item['member_info'] = Db::name('member')->find($item['member_id']);
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                return $item;
            });
        msg(0, "查询成功", $order_list);
    }

    //核销码核销
    public function verifyOrderDigits(){
        $data = $this->request->param();
        $order_id = $data['order_id'];
        $write_code_digits = $data['write_code_digits'];
        Db::startTrans();
        try {
            $result = verifyGoodsOrder($order_id, 2, $this->shopId, $write_code_digits);
            Db::commit();
            msg(0, '核销完成', $result);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //商家扫码核销
    public function verifyOrder()
    {
        $data = $this->request->param();
        Db::startTrans();
        try {
            $order_id = $data['order_id'];
            $result = verifyGoodsOrder($order_id, 1, $this->shopId, '');
            Db::commit();
            msg(0, '核销完成', $result);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //顶部收益
    public function shopMoneyTop(){
        $return['a']=$this->shopInfo['money'];
        //待入账 提现中 已提现
        $b=Db::name('order')->where([
            'shop_id'=>$this->shopId,
            'status'=>8
        ])->sum('shop_get_money');
        $c = Db::name('shop_finance')->where([
            ['shop_id', '=', $this->shopId],
            ['type', '=', 2],
            ['is_kuan', '=', 0],
        ])->sum('money');
        $d = Db::name('shop_finance')->where([
            ['shop_id', '=', $this->shopId],
            ['type', '=', 2],
            ['is_kuan', '=', 1],
        ])->sum('money');
        $return['b']=$b;
        $return['c']=$c;
        $return['d']=$d;
        msg(0, '更新完成', $return);
    }
    //商家提现
    public function ShopTixian()
    {
        $data = $this->request->param();
        if ($data['money'] <= 0) {
            msg(-1, '输入正确值', '');
        }
        if ($data['money'] > $this->shopInfo['money']) {
            msg(-2, '提现金额不能大于余额', '');
        }
        if ($data['money'] < $this->shopInfo['min_tx']) {
            msg(-3, '提现金额低于最小金额', '');
        }
        Db::startTrans();
        try {
            $insert_data = array(
                'shop_id' => $this->shopId,
                'money' => $data['money'],
                'is_count'=>2,
                'type' => 2,
                'tixian_type' => $data['tixian_type'], //1银行 2支付宝 3微信
                'tixian_account' => $data['tixian_account'], //提现账号
                'tixian_name' => $data['tixian_name'],//提现人姓名
                'tixian_bank' => $data['tixian_bank'],//提现银行名（银行提现使用）
                'desc' => '提现',
                'status' => 0,//'0未完成 待审核 1已完成 审核完成 提现使用',
                'create_time' => time(),
            );
//            $insert_data['shou_money'] = round($data['money'] * (0.07), 2);
            $insert_data['shou_money']=0;
            $insert_data['true_money'] = $data['money'] - $insert_data['shou_money'];
            $insert_id = Db::name('shop_finance')->insertGetId($insert_data);
            Db::name('shop')
                ->where('id', '=', $this->shopId)
                ->setDec('money', $data['money']);
            Db::commit();
            msg(0, '申请成功', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //收入明细
    public function shopMoneyList(){
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $type = $data['type']; //0全部 1收入 2提现
        //todo 修改时间格式 2025-05
        $where[]=['shop_id','=',$this->shopId];
        $time=$data['time'];
        if (isset($time)&&$time){
            $order_time=getStrtoTime($time);
            $where[] = ['create_time', 'between', [$order_time[0],$order_time[1]]];
        }

        if ($type==0){
            $where[]=['type','in',[1,2]];
        }
        if ($type==1){
            $where[]=['type','in',[1]];
        }
        if ($type==2){
            $where[]=['type','in',[2]];
        }
        $shop_list = Db::name('shop_finance')
            ->where($where)
            ->order('create_time desc')
            ->paginate($length, false, [
                'page' => $page
            ])->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                return $item;
            });
        msg(0, '查询成功', $shop_list);
    }
    //订单收益 核销订单
    public function getOrderDate(){
        $data = $this->request->param();
        $time=$data['time'];
        $where[] = ['shop_id', '=', $this->shopId];
        if (isset($time)&&$time){
            $order_time=getStrtoTime($time);
            $where[] = ['pay_time', 'between', [$order_time[0],$order_time[1]]];
        }
        $money=Db::name('shop_finance')->where([
            ['type','=',1],
            ['shop_id','=',$this->shopId]
        ])->sum('money');
        $order_num_dai=Db::name('order')->where([
            ['shop_id','=',$this->shopId],
            ['status','in',[8]]
        ])->where($where)->count();
        $order_num=Db::name('order')->where([
            ['shop_id','=',$this->shopId],
            ['status','in',[4,5]]
        ])->count();
        $return['money']=$money;
        $return['order_num']=$order_num;
        $return['order_num_dai']=$order_num_dai;
        msg(0, '查询成功', $return);
    }
    //获取待核销数量
    public function getHxOrder(){
        $order_num_dai=Db::name('order')->where([
            ['shop_id','=',$this->shopId],
            ['status','in',[8]]
        ])->count();
        msg(0, '查询成功', $order_num_dai);
    }


}